﻿using EMoney.IStock.Client.Core;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.Business.Account
{
    /// <summary>
    /// 账号相关业务处理类
    /// </summary>
    public class AccountBll
    {
        /// <summary>
        /// 设置发送验证码的唯一标识
        /// </summary>
        public void SetSendCookieVal()
        {
            DateTime dtNow = DateTime.Now;

            string sendName = string.Concat(SendConstDefine.SENDNAME_CACHED_KEY, ".", StringHelper.FormatDateTime(dtNow));
            string sendKey = string.Concat(SendConstDefine.SENDKEY_CACHED_KEY, ".", StringHelper.FormatDateTime(dtNow));

            string cookieVal = CookieHelper.GetCookie(sendName, sendKey);

            if (string.IsNullOrEmpty(cookieVal))
            {
                string uniqueNo = StringHelper.GetUniqueNo();

                CookieHelper.SetCookie(sendName, sendKey, uniqueNo, 24);
            }
        }

        /// <summary>
        /// 获取发送验证码的唯一标识
        /// </summary>
        /// <returns></returns>
        public string GetSendCookieVal()
        {
            DateTime dtNow = DateTime.Now;

            string sendName = string.Concat(SendConstDefine.SENDNAME_CACHED_KEY, ".", StringHelper.FormatDateTime(dtNow));
            string sendKey = string.Concat(SendConstDefine.SENDKEY_CACHED_KEY, ".", StringHelper.FormatDateTime(dtNow));

            return CookieHelper.GetCookie(sendName, sendKey);
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="account">账号（邮箱或手机号）</param>
        /// <param name="flag">1:手机 2:邮箱</param>
        /// <param name="sendCookieVal"></param>
        /// <returns></returns>
        public string SendCode(string account, string flag, string sendCookieVal)
        {
            string ret = string.Empty;
            string code = string.Empty;
            string cacheCode = SendBll.GetCode(account); //获取该账号有效期内发送的验证码

            if (string.IsNullOrEmpty(cacheCode))
            {
                code = new Random().Next(100000, 999999).ToString();
            }
            else
            {
                code = cacheCode;
            }

            switch (flag)
            {
                case "1":
                    if (SendBll.SendSms(sendCookieVal, account, string.Concat("您的验证码是:", code, "（30分钟内有效），请将该验证码填写到页面的对应位置")))
                    {
                        SendBll.CacheCode(account, code);
                        ret = "1";
                    }
                    else
                    {
                        ret = "0";
                    }
                    break;
                case "2":
                    string subject = string.Concat("爱炒股用户邮件验证码:", code);
                    string body = "<strong>尊敬的爱炒股用户</strong>:<br /><p><span style='white-space:pre'>	</span>&nbsp;&nbsp;你的验证码为：<strong>" + code + 
                        "&nbsp;</strong>（30分钟内有效），请将验证码填写到页面中的指定位置。 <br /><span style='white-space:pre'>	</span>&nbsp;&nbsp;感谢你使用益盟爱炒股软件！如果你不是爱炒股用户，请你忽略本邮件，给你带来的不便，我们深表歉意。（该邮件不需要回复）</p><p><span style='white-space:pre'></span><strong>益盟爱炒股</strong></p>";

                    if (SendBll.SendEmail(sendCookieVal, account, subject, body))
                    {
                        SendBll.CacheCode(account, code);
                        ret = "1";
                    }
                    else
                    {
                        ret = "0";
                    }
                    break;
                default:
                    break;
            }

            return ret;
        }
    }
}
